#include "ts.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/input.h>
#include "lcd.h"

extern int touch_fd;

struct touch_index get_xy()
{
	struct touch_index read_xy = {-1,-1};
	//定义一个结构体去保存读取到的数据
	struct input_event ts;
	//不停的读取触摸事件
	while(1)
	{
		//读
		read(touch_fd,&ts,sizeof(struct input_event));
		if(ts.type == EV_KEY && ts.code == BTN_TOUCH)
		{
			if(ts.value == 1)
			{
				printf("press\n");
			}
			else
			{
				printf("relse\n");
			}
		}
		
		if(ts.type == EV_ABS)//获取xy的坐标
		{
			if(ts.code == ABS_X)
			{
				read_xy.x = ts.value*800/1024;
			}
			if(ts.code == ABS_Y)
			{
				read_xy.y = ts.value*480/600;
			}
		}
		
		if(read_xy.x>=0&&read_xy.y>=0)//得到坐标
		{
			break;
		}
	}
    return read_xy;
}

struct touch_index slide_touch()
{
    struct touch_index xy;
    int x1_read = -1,y1_read = -1;
    int x2_read = -1,y2_read = -1;

	struct touch_index read_xy = {-1,-1};
    int touch_fd = open("/dev/input/event0",O_RDWR);
    if(touch_fd == -1)
    {
        perror("open failed");
    }

    struct input_event ts;
    while(1)
    {
        read(touch_fd,&ts,sizeof(struct input_event));
        if(ts.type == EV_KEY && ts.code == BTN_TOUCH)
        {
            if(ts.value == 1)
            {
                printf("press\n");
            }
            else
            {
                printf("relae\n");
                // if(x2_read >= 0 && y2_read >= 0)
                // {
                    // // xy.x = x2_read;
                    // if(abs(x1_read-x2_read)>abs(y1_read - y2_read)&&x1_read>x2_read)
                    // {
                    //     printf("lift ");
                    // }
                    // else if(abs(x1_read-x2_read)>abs(y1_read - y2_read)&&x2_read>x1_read)
                    // {
                    //     printf("right ");
                    // }
                    // else if(abs(y1_read - y2_read)>abs(x1_read-x2_read)&&y2_read<y1_read)
                    // {
                    //     printf("up\n");
                    // }
                    // else if(abs(y1_read - y2_read)>abs(x1_read-x2_read)&&y2_read>y1_read)
                    // {
                    //     printf("down\n");
                    // }

                    // if(abs(y1_read-y2_read)>(x1_read-x2_read)&&y2_read<y1_read)
                    // {
                    //     xy.d = 1;
                    // }
                // }
                // else
                // {
                //     xy.x = x1_read;
                //     xy.y = y1_read;
                // }
            }
        }

        if(ts.type == EV_ABS)
        {
            if(ts.code == ABS_X)
            {
                if(x1_read >= 0 && y1_read >= 0)
                {
                    read_xy.x = ts.value*800/1024;
                }
                else
                {
                    read_xy.x = ts.value*800/1024;
                }
            }
            if(ts.code == ABS_Y)
            {
                if(x1_read >= 0 && y1_read >= 0)
                {
                    read_xy.y = ts.value*480/600;
                }
                else
                {
                    read_xy.y = ts.value*480/600;
                }
            }

        }

        if(read_xy.x >= 0 && read_xy.y >= 0)
        {
            return read_xy;
        }

    }
}
